package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 * Calculator
 *
 * @author Joonas Vali
 */
public class CalculatorGUI {
	public static JTextField screen;
	public static Calculator calc;
	
	
	
	
	private static void createAndShowGUI() {
		
		
        final JFrame frame = new JFrame("Photos, The Up-To-Date Calculator");        
        final Banner banner = new Banner(frame.getWidth());
        final ComboBoxModel comboModel;
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(new Point(200, 200));        
        frame.setMinimumSize(new Dimension(350, 200));
        
        frame.getContentPane().addHierarchyBoundsListener(new HierarchyBoundsListener(){
			@Override
			public void ancestorMoved(HierarchyEvent e) {	}

			@Override
			public void ancestorResized(HierarchyEvent e) {				
				banner.setSize(new Dimension(frame.getWidth(), banner.getHeight()));
				banner.initOffscreen();
			}        	
        });
        
        BorderLayout frameGrid = new BorderLayout();             
        frame.setLayout(frameGrid);              
        
        JPanel controlPanel = new JPanel(new GridLayout(0,4));
        JPanel screenPanel = new JPanel(new BorderLayout());        
        screenPanel.setBackground(new Color(220,220,230));        
       
        screen = new JTextField();
        screen.setEditable(false);
        screen.setBackground(Color.WHITE);
        screen.setHorizontalAlignment(JTextField.RIGHT);    
        calc = new Calculator(screen);
        
        comboModel = new DefaultComboBoxModel(NewsType.values());        
        
        JButton clear = new JButton("C");
        JButton res1 = new JButton("");        
        JButton res2 = new JButton("");
        JButton res3 = new JButton("");        
        JButton res4 = new JButton("");
        JComboBox newsComboBox = new JComboBox(comboModel);
        JButton[] numbers = new JButton[10];        
        JButton butDivide = new JButton("/");        
        JButton butMulti = new JButton("*");        
        JButton butAdd = new JButton("+");
        JButton butComma = new JButton(".");        
        JButton butMinus = new JButton("-");
        JButton butEquals = new JButton("=");
        JButton leftBrace = new JButton("(");
        JButton rightBrace = new JButton(")");
        for(int i =0 ; i < numbers.length; i++){
        	numbers[i] = new JButton(Integer.toString(i));
        	numbers[i].addActionListener(calc.getInputListener(Integer.toString(i)));
        }
        clear.addActionListener(calc.getClearListener());
        //addListener(res1);
        //addListener(res2);        
        butComma.addActionListener(calc.getInputListener("."));
        butDivide.addActionListener(calc.getInputListener("/"));
        butMinus.addActionListener(calc.getInputListener("-"));
        butAdd.addActionListener(calc.getInputListener("+"));
        butMulti.addActionListener(calc.getInputListener("*"));
        leftBrace.addActionListener(calc.getInputListener("("));
        rightBrace.addActionListener(calc.getInputListener(")"));
        butEquals.addActionListener(calc.getResultListener());
        
        
        controlPanel.add(clear);
        controlPanel.add(res1);
        controlPanel.add(res2);
        controlPanel.add(newsComboBox);
        controlPanel.add(res3);
        controlPanel.add(res4);
        controlPanel.add(leftBrace);
        controlPanel.add(rightBrace);
        controlPanel.add(numbers[1]);
        controlPanel.add(numbers[2]);
        controlPanel.add(numbers[3]);
        controlPanel.add(butDivide);
        controlPanel.add(numbers[4]);
        controlPanel.add(numbers[5]);
        controlPanel.add(numbers[6]);
        controlPanel.add(butMulti);
        controlPanel.add(numbers[7]);
        controlPanel.add(numbers[8]);
        controlPanel.add(numbers[9]);
        controlPanel.add(butAdd);
        controlPanel.add(butComma);
        controlPanel.add(numbers[0]);
        controlPanel.add(butMinus);
        controlPanel.add(butEquals);            
        
        res1.setEnabled(false);
        res2.setEnabled(false);
        res3.setEnabled(false);
        res4.setEnabled(false);
        
        
        comboModel.addListDataListener(new ListDataListener(){
			@Override
			public void contentsChanged(ListDataEvent e) {
				banner.changeContent((NewsType)comboModel.getSelectedItem());					
			}
			
			@Override
			public void intervalAdded(ListDataEvent e) {	}
			
			@Override			
			public void intervalRemoved(ListDataEvent e) {		}        	
        });
       
        banner.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        banner.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent e) {
				String[] options = {"Read in browser",
	            "Cancel"};
				URI activeLink = banner.active.getUri();
				int n = JOptionPane.showOptionDialog(frame, banner.active.slicedDescription(15)+
						"\n\nWould you like to read about it from your browser?",
						"Open browser?",
						JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE,
						null,
						options,
						options[1]
				);
				if(n == 0)
					try {
						Desktop.getDesktop().browse(activeLink);
					}
					catch (IOException e1) {
						JOptionPane.showMessageDialog(frame,
							    "Unable to open browser.",
							    "Error",
							    JOptionPane.ERROR_MESSAGE
						);
					}

			}
			@Override
			public void mouseEntered(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseReleased(MouseEvent e) {}        	
        });
        
        screenPanel.add(banner, BorderLayout.CENTER);
        screenPanel.add(screen, BorderLayout.SOUTH);
        frame.add(screenPanel,BorderLayout.PAGE_START);
        frame.add(controlPanel,BorderLayout.PAGE_END);
        frame.pack();
        frame.setVisible(true);
        
     }
	
	public static void main(String[] args) {
	    SwingUtilities.invokeLater(new Runnable() {
	       public void run() {
	          createAndShowGUI();
	       }
	    });

    } 	
	
	
}
